import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace SubstituteMaterial {
  export interface AddRequest {
    mbomItemId: number;
    substituteMaterialId: number;
    substituteRatio: number;
    priority: number;
    validFrom?: string;
    validTo?: string;
    remark?: string;
  }

  export interface UpdateRequest {
    substituteRatio: number;
    priority: number;
    validFrom?: string;
    validTo?: string;
    remark?: string;
  }

  export interface RejectRequest {
    rejectReason: string;
  }

  export interface ApplyToWorkOrderRequest {
    workOrderId: number;
    actualQuantity: number;
  }

  export interface ViewEntity {
    id: number;
    mbomId: number;
    mbomCode: string;
    mbomName: string;
    mbomItemId: number;
    mainMaterialId: number;
    mainMaterialCode: string;
    mainMaterialName: string;
    substituteMaterialId: number;
    substituteMaterialCode: string;
    substituteMaterialName: string;
    substituteRatio: number;
    priority: number;
    validFrom?: string;
    validTo?: string;
    status: string;
    rejectReason?: string;
    createdTime: string;
    updatedTime: string;
    createdBy: string;
    updatedBy: string;
  }

  export interface ListQuery {
    mainMaterialCode?: string;
    substituteMaterialCode?: string;
    status?: string;
    mbomId?: number;
    mbomItemId?: number;
  }
}

/**
 * 替代料API
 */
export const substituteMaterialApi = {
    /**
     * 添加替代料
     */
    add: (params: SubstituteMaterial.AddRequest) => {
      return http.post<void>(`/substituteMaterial/add`, params);
    },
  
    /**
     * 更新替代料
     */
    update: (id: number, params: SubstituteMaterial.UpdateRequest) => {
      return http.put<void>(`/substituteMaterial/${id}`, params);
    },
  
    /**
     * 移除替代料
     */
    remove: (id: number) => {
      return http.delete<void>(`/substituteMaterial/${id}`);
    },
  
    /**
     * 审核通过替代料
     */
    approve: (id: number) => {
      return http.put<void>(`/substituteMaterial/${id}/approve`);
    },
  
    /**
     * 审核拒绝替代料
     */
    reject: (id: number, params: SubstituteMaterial.RejectRequest) => {
      return http.put<void>(`/substituteMaterial/${id}/reject`, params);
    },
  
    /**
     * 工单应用替代料
     */
    applyToWorkOrder: (id: number, params: SubstituteMaterial.ApplyToWorkOrderRequest) => {
      return http.put<void>(`/substituteMaterial/${id}/applyWorkorder`, params);
    },
  
    /**
     * 分页查询替代料列表
     */
    getList: (params: any) => {
      return http.get<any>(`/substituteMaterial/list`, params);
    },
  
    /**
     * 根据ID查询替代料详情
     */
    getById: (id: number) => {
      return http.get<SubstituteMaterial.ViewEntity>(`/substituteMaterial/${id}`);
    },
  
    /**
     * 根据MBOM ID查询替代料列表
     */
    getByMbomId: (mbomId: number) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/mbom/${mbomId}`);
    },
  
    /**
     * 根据MBOM项ID查询替代料列表
     */
    getByMbomItemId: (mbomItemId: number) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/mbomItem/${mbomItemId}`);
    },
  
    /**
     * 根据状态查询替代料列表
     */
    getByStatus: (status: string) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/status/${status}`);
    },
  
    /**
     * 根据主料编码查询替代料列表
     */
    getByMainMaterialCode: (mainMaterialCode: string) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/mainMaterial/${mainMaterialCode}`);
    },
  
    /**
     * 根据替代料编码查询替代料列表
     */
    getBySubstituteMaterialCode: (substituteMaterialCode: string) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/substituteMaterial/${substituteMaterialCode}`);
    },
  
    /**
     * 查询可用的替代料
     */
    getAvailableSubstitutes: (mbomItemId: number) => {
      return http.get<SubstituteMaterial.ViewEntity[]>(`/substituteMaterial/available/${mbomItemId}`);
    }
  };
  